package com.threeox.commonlibrary.ui.view.engineview.listmodel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.adapter.base.CommonAdapter;
import com.threeox.commonlibrary.adapter.base.CommonExpandAdapter;
import com.threeox.commonlibrary.adapter.base.IAdapter;
import com.threeox.commonlibrary.entity.engine.Lifecycle;
import com.threeox.commonlibrary.entity.engine.ReloadType;
import com.threeox.commonlibrary.entity.engine.event.BaseEventMessage;
import com.threeox.commonlibrary.entity.engine.event.EventMessage;
import com.threeox.commonlibrary.entity.engine.model.listmodel.LetterViewDisplayPosition;
import com.threeox.commonlibrary.entity.engine.model.listmodel.ListModelMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.entity.engine.request.sqlite.OperationTypeMsg;
import com.threeox.commonlibrary.entity.engine.request.sqlite.SqliteRequestMsg;
import com.threeox.commonlibrary.inter.navigation.INavigationAdapter;
import com.threeox.commonlibrary.request.IRequest;
import com.threeox.commonlibrary.ui.view.engineview.base.CommonModelView;
import com.threeox.commonlibrary.ui.view.inter.engine.base.IBaseExtend;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.IListModelExtend;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.INavigationView;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.OnTouchNavigationChangedListener;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IPullToRefLayout;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IPullable;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.OnRefreshListener;
import com.threeox.commonlibrary.ui.view.pullrefresh.PullToRefreshExpandListView;
import com.threeox.commonlibrary.ui.view.pullrefresh.PullToRefreshGridView;
import com.threeox.commonlibrary.ui.view.pullrefresh.PullToRefreshListView;
import com.threeox.commonlibrary.ui.view.pullrefresh.base.PullToRefreshLayout;
import com.threeox.commonlibrary.util.CommonConstant.HeaderFooter;
import com.threeox.commonlibrary.util.CommonConstant.ListViewType;
import com.threeox.commonlibrary.util.JSONUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.ToastUtils;
import com.threeox.utillibrary.util.res.LayoutUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @ClassName: ListModelBaseView
 *
 * @Description: Todo(列表ModelBaseView)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/22 11:28
 *
 * @version 1.0
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ListModelBaseView extends CommonModelView implements OnRefreshListener, OnClickListener, OnItemClickListener,
		OnTouchNavigationChangedListener, AdapterView.OnItemLongClickListener, AbsListView.OnScrollListener {

    public static final String REQPARAM = "REQPARAM";// 请求的参数

    /*====================数据配置变量===========================*/
	private List mDatas;
	private String mViewType;
	private IAdapter mAdapter;
	private IRequest mIRequest;
    /*====================数据配置变量===========================*/
    /*====================布局配置变量===========================*/
    private IPullable mPullableView;// 中间主要的View
    private LinearLayout mHeaderLayout;
	private LinearLayout mListModelLayout;
    private INavigationView mNavigationView;// 侧边导航栏
    private PullToRefreshLayout mPullToRefLayout;
    /*====================布局配置变量===========================*/
    private ListModelMsg mListModelMessage;
	private IListModelExtend mListModelExtend;
	private com.threeox.commonlibrary.inter.OnItemClickListener mItemClickListener;

	protected BaseRequestMsg mBaseRequestMsg;// 请求对象
    protected EventMessage mItemClickEvent;// 列表单击事件

	protected boolean mIsHideFooter = true;// 是否隐藏底部

    public ListModelBaseView(Context context) {
		this(context, null);
	}

	public ListModelBaseView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ListModelBaseView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void initView() {
		super.initView();
		LayoutUtils.inflate(mContext, R.layout.view_listmodel, this);
		mHeaderLayout = findViewById(R.id.id_listmodel_header);
		mContainerLayout = findViewById(R.id.id_listmodel_container);
		mListModelLayout = findViewById(R.id.id_listmodel_layout);
		mContainerLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  LayoutParams.MATCH_PARENT, 1));
	}

	/**
	 * 初始化数据
	 *
	 * @param intent
	 */
	@Override
	public void initData(Intent intent) {
		super.initData(intent);
		try {
			mFileName = mIntent.getIntExtra(FILENAMEMODE, -1);
			JSONObject param = null;
			if (EmptyUtils.isNotEmpty(mIntent.getStringExtra(REQPARAM))) {
				param = JSON.parseObject(mIntent.getStringExtra(REQPARAM));
			} else {
				param = new JSONObject();
			}
			initData(mFileName, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *
	 * @param fileName
	 */
	public void initData(int fileName) {
		initData(fileName, new JSONObject());
	}

	/**
	 * 初始化数据
	 *
	 * @param fileName
	 * @param param
	 */
	private void initData(int fileName, JSONObject param) {
		try {
            if (null != mListModelMessage) {
                setBaseModelMsg(mListModelMessage);
				initRequestMsg(param);
				fillInView();
                initExtend();
                initAdapter();
                initPublic();
                boolean isExec = true;
                if (mListModelExtend != null){
                    isExec = mListModelExtend.init(getContext(), this);
                }
                initConfig();
				if (isExec && mListModelMessage.isExecute()) {
					exec(true);
				}
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public ListModelMsg initModelMessage(int fileName) {
	    if (mListModelMessage == null) {
            this.mFileName = fileName;
            if (mFileName == -1) {
                ToastUtils.showLongToastSafe(mContext, NOTMODELFILE);
                return null;
            }
            mListModelMessage = mCommonConfig.getListModelMsg(mFileName);
        }
		return mListModelMessage;
	}

	@Override
	protected void initExtend() {
		if (mBaseExtend != null && mBaseExtend instanceof IListModelExtend) {
			this.mListModelExtend = (IListModelExtend) mBaseExtend;
		}
	}

	/**
	 * 初始化列表Layout
	 */
	private void fillInView() {
		initCommonView();
        initPullToRefLayout();
		initNavigationView();
	}

	/**
	 * 初始化请求对象
	 *
	 * @param param
	 */
	protected void initRequestMsg(JSONObject param) {
		initRequestMsg();
		mBaseRequestMsg = mListModelMessage.getBaseRequestMsg();
		if (mBaseRequestMsg != null) {
			mBaseRequestMsg.getRequestParam().putAll(param);
			if (mBaseRequestMsg instanceof SqliteRequestMsg) {
				((SqliteRequestMsg) mBaseRequestMsg).setOperationType(OperationTypeMsg.SELECTLIST);
			}
		}
	}

    /**
     * 初始化可以下拉刷新的View
     */
    private void initPullToRefLayout() {
        try {
            Integer headerLayout = mListModelMessage.getHeaderLayoutId();
            if (EmptyUtils.isNotEmpty(headerLayout)) {
                addHeaderView(headerLayout);
            }
            mViewType = mListModelMessage.getViewType();
            if (ListViewType.GRIDVIEW.equals(mViewType)) {
                mPullableView = new PullToRefreshGridView(mContext);
                mPullToRefLayout = new PullToRefreshLayout(mContext, null, 0, (View) mPullableView);
                ((GridView) mPullableView).setOnItemClickListener(this);
				((GridView) mPullableView).setOnItemLongClickListener(this);
            } else if (ListViewType.EXPANLISTVIEW.equals(mViewType)) {
                mPullableView = new PullToRefreshExpandListView(mContext);
                mPullToRefLayout = new PullToRefreshLayout(mContext, null, 0, (View) mPullableView);
            } else if (ListViewType.CUSTOMVIEW.equals(mViewType)) {
                Class customViewClass = mListModelMessage.getCustomViewClass();
                if (customViewClass != null) {
                    Constructor constructor = customViewClass.getConstructor(Context.class);
                    mPullableView = (IPullable) constructor.newInstance(mContext);
                    mPullToRefLayout = new PullToRefreshLayout(mContext, null, 0, (View) mPullableView);
                    if (mPullableView != null) {
                        if (mPullableView instanceof GridView) {
                            ((GridView) mPullableView).setOnItemClickListener(this);
                            ((GridView) mPullableView).setOnItemLongClickListener(this);
                        } else if (mPullableView instanceof ListView) {
                            ((ListView) mPullableView).setOnItemClickListener(this);
                            ((ListView) mPullableView).setOnItemLongClickListener(this);
                        }
                    }
                }
            } else {
                mPullableView = new PullToRefreshListView(mContext);
                mPullToRefLayout = new PullToRefreshLayout(mContext, null, 0, (View) mPullableView);
                ((ListView) mPullableView).setOnItemClickListener(this);
                ((ListView) mPullableView).setOnItemLongClickListener(this);
            }
            if (mPullableView instanceof AbsListView) {
                ((AbsListView) mPullableView).setOnScrollListener(this);
            }
            mPullToRefLayout.setId(R.id.id_pulltorefreshlayout);
            mPullToRefLayout.setListModelMsg(mListModelMessage);
            mPullToRefLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0 , 1));
            mListModelLayout.addView(mPullToRefLayout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	/**
	 * 初始化导航条View
	 */
	private void initNavigationView() {
        if (mListModelMessage.isShowNavigationView()) {
            mNavigationView = mOverallInitView.initListNavigationView();
            if (mNavigationView != null && mNavigationView instanceof View) {
                TextView mDialogView = mOverallInitView.initNavigationDialogView();
                if (mDialogView != null) {
                    mNavigationView.setDialogView(mDialogView);
                }
                RelativeLayout.LayoutParams dialogLayoutParams = mLayoutParamsUtils.getRelativeLayoutParams(80, 80);
                dialogLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
                mDialogView.setLayoutParams(dialogLayoutParams);
                mContainerLayout.addView(mDialogView);
                View view = ((View) mNavigationView);
                LetterViewDisplayPosition displayPosition = mListModelMessage.getDisplayPosition();
                RelativeLayout.LayoutParams layoutParams = null;
                if (LetterViewDisplayPosition.TOP == displayPosition || LetterViewDisplayPosition.BOTTOM == displayPosition) {
                    layoutParams = mLayoutParamsUtils.getRelativeLayoutParams(LayoutParams.MATCH_PARENT, 25);
                } else {
                    layoutParams = mLayoutParamsUtils.getRelativeLayoutParams(25, LayoutParams.MATCH_PARENT);
                }
                layoutParams.addRule(mListModelMessage.getAlignDisplayPosition());
                view.setLayoutParams(layoutParams);
                mContainerLayout.addView(view);
                String[] charArray = mListModelMessage.getCharArray();
                if (EmptyUtils.isNotEmpty(charArray)) {
                    mNavigationView.setCharArray(charArray);
                }
                mNavigationView.setOnTouchNavigationChangedListener(this);
            }
        }
	}

    @Override
    protected void initBaseEventMessage() {
        super.initBaseEventMessage();
        BaseEventMessage itemClickEvent = mListModelMessage.getItemClickEvent();
        if (null != itemClickEvent) {
			mItemClickEvent = mEventFactory.convertEventMessage(itemClickEvent);
			mItemClickEvent.initAllParams(mIntent);
		}
    }

	/**
	 * 初始化配置
	 *
	 * @return
     */
	public ListModelBaseView initConfig() {
		try {
		    if (mPullableView instanceof GridView) {
                ((GridView) mPullableView).setAdapter((ListAdapter) mAdapter);
                ((GridView) mPullableView).setNumColumns(mListModelMessage.getNumColumns());
            } else if (mPullableView instanceof ExpandableListView) {
				((ExpandableListView) mPullableView).setAdapter((BaseExpandableListAdapter) mAdapter);
            } else if (mPullableView instanceof ListView) {
				((ListView) mPullableView).setAdapter((ListAdapter) mAdapter);
            }
			/*if (ListViewType.GRIDVIEW.equals(mViewType)) {
                ((GridView) mPullableView).setAdapter((ListAdapter) mAdapter);
                ((GridView) mPullableView).setNumColumns(mListModelMessage.getNumColumns());
			} else if (ListViewType.EXPANLISTVIEW.equals(mViewType)) {
                ((ExpandableListView) mPullableView).setAdapter((BaseExpandableListAdapter) mAdapter);
			} else {
                ((ListView) mPullableView).setAdapter((ListAdapter) mAdapter);
			}*/
			// RequestUtil 初始化
			Class requestClass = mListModelMessage.getRequestClass();
			Constructor requestConstructor = requestClass.getConstructor(List.class, IAdapter.class, ListModelMsg.class);
			mIRequest = (IRequest) requestConstructor.newInstance(mDatas, mAdapter, mListModelMessage);
            // mIRequest.setReloadView(mReloadView);
            mIRequest.setListModelBaseView(this);
            mListModelMessage.getHeaderFooterType(mPullToRefLayout);
			setFirstPage();
			mPullToRefLayout.setOnRefreshListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this;
	}

	/**
	 * 初始化Adapter
	 *
	 * @throws Exception
	 */
	private void initAdapter() throws Exception {
        mDatas = new ArrayList();
		// Adapter 初始化
		Class<?> cls = mListModelMessage.getAdapterClass();
		if (ListViewType.EXPANLISTVIEW.equals(mListModelMessage.getViewType())) {
			Method method = cls.getMethod("init", Context.class, List.class, String.class, Integer.class, Integer.class);
			mAdapter = (IAdapter) method.invoke(cls.newInstance(), getContext(), mDatas,  mListModelMessage.getChildName(), mListModelMessage.getLayoutId(),
					mListModelMessage.getChildLayoutId());
			if (mAdapter instanceof CommonExpandAdapter) {
				((CommonExpandAdapter) mAdapter).setListView((ExpandableListView) mPullableView);
			}
		} else {
			if (mListModelMessage.getLayoutId() == null) {
                Method method = cls.getMethod("init", Context.class, List.class);
				mAdapter = (CommonAdapter) method.invoke(cls.newInstance(), getContext(), mDatas);
			} else {
				Method method = cls.getMethod("init", Context.class, List.class, Integer.class);
				mAdapter = (CommonAdapter) method.invoke(cls.newInstance(), getContext(), mDatas, mListModelMessage.getLayoutId());
			}
		}
		if (mAdapter != null && mAdapter instanceof INavigationAdapter) {
            ((INavigationAdapter) mAdapter).setItemFirstId(mListModelMessage.getItemFirstId());
        }
	}

	@Override
	protected boolean verify(JSONObject value) {
		return true;
	}

	/**
	 * 设置第一页数据
	 */
	private void setFirstPage() {
		try {
			if (EmptyUtils.isNotEmpty(mListModelMessage.getPageName())) {
				int firstPage = 0;
				if (mBaseRequestMsg != null) {
					firstPage = Integer.parseInt(String.valueOf(JSONUtils.getJSONValue(mBaseRequestMsg.getRequestParam(), mListModelMessage.getPageName())));
				}
				mIRequest.setFirstPage(firstPage);
			}
		} catch (Exception e) {
			mIRequest.setFirstPage(0);
		}
	}

	/**
	 * 下拉事件监听
	 */
	@Override
	public void onDownPullRefresh() {
		if (mListModelExtend != null && !mListModelExtend.onDownPullRefresh()) {
            mPullToRefLayout.refreshFinish(IPullToRefLayout.SUCCEED, null);
            return;
        }
		exec(true);
	}

	/**
	 * 上拉加载更多事件监听
	 */
	@Override
	public void onLoadingMore() {
		if (mListModelExtend != null && !mListModelExtend.onLoadingMore()) {
            mPullToRefLayout.loadmoreFinish(IPullToRefLayout.SUCCEED, null);
            return;
        }
		exec(false);
	}

	/**
	 * 执行请求
	 *
	 * @param isFirst  是否第一次请求
	 */
	public void exec(boolean isFirst) {
        if (mListModelExtend != null && !mListModelExtend.exec(isFirst, mBaseRequestMsg)) {
            return;
        }
		requestIn();
		if (isFirst) {
			if (HeaderFooter.HideAll.equals(mListModelMessage.getHeaderFooterType()) || HeaderFooter.HideHeader.equals(mListModelMessage.getHeaderFooterType())) {
				if (mBaseRequestMsg != null && (mBaseRequestMsg instanceof SqliteRequestMsg)) {
					setFirstJoin(true);
				}
			}
			mIRequest.execFirst(mBaseRequestMsg);
		} else {
			mIRequest.execNext(mBaseRequestMsg);
		}
	}

	/**
	 * 添加请求数据
	 *
	 * @param key
	 * @param val
	 */
	public void putRequestParam(String key, Object val) {
		if (mBaseRequestMsg != null) {
			mBaseRequestMsg.putRequestParam(key, val);
		}
	}

	/**
	 * 根据Key只得到指定的参数
	 *
	 * @param key
	 * @return
	 */
	public <T extends Object> T getRequestParam(String key) {
		if (mBaseRequestMsg != null) {
			return (T) mBaseRequestMsg.getRequestParam(key);
		}
		return null;
	}

	/**
	 * 根据Key 删除
	 * @param key
	 */
	public void removeRequestParam(String key) {
		if (mBaseRequestMsg != null) {
			mBaseRequestMsg.removeRequestParam(key);
		}
	}

	/**
	 * 清空Adapter 列表
	 */
	public void clearAdapter() {
		if (mAdapter != null) {
			mAdapter.removeAll();
		}
	}

	/**
	 * 给Adapter 添加集合数据
	 * @param data
	 */
	public void addAdapter(List data) {
		if (mAdapter != null) {
			mAdapter.add(data);
		}
	}

	public LinearLayout getListModelLayout() {
		return mListModelLayout;
	}

	public <T extends View> T getMainView() {
		if (mPullToRefLayout != null) {
			return mPullToRefLayout.getMainView();
		}
		return null;
	}

	public ListModelMsg getListModelMessage() {
		return mListModelMessage;
	}

	public PullToRefreshLayout getPullToRefLayout() {
		return mPullToRefLayout;
	}

	public List getDatas() {
		return mDatas;
	}

	public IAdapter getAdapter() {
		return mAdapter;
	}

	@Override
	public void onClick(View v) {
		exec(true);
	}

    /**
     * 设置是否清除
	 *
     * @param isClear
     */
	public void setIsClear(boolean isClear) {
		mIRequest.setIsClear(isClear);
	}

	/**
	 * 添加布局
	 *
	 * @param layoutId
	 * @return
	 */
	public ListModelBaseView addHeaderView(int layoutId) {
		View view = LayoutUtils.inflate(getContext(), layoutId);
        mHeaderLayout.addView(view);
		return this;
	}

	public ListModelBaseView addHeaderView(View v) {
        mHeaderLayout.addView(v, 0);
		return this;
	}

	public void setFirstJoin(boolean isFirstJoin) {
		mIRequest.setFirstJoin(isFirstJoin);
	}

	public void setOnItemClickListener(com.threeox.commonlibrary.inter.OnItemClickListener listener) {
		this.mItemClickListener = listener;
	}

	public IListModelExtend getListModelExtend() {
		return mListModelExtend;
	}

    /**
     * 得到一列数据
     *
     * @param position
     * @return
     */
	public Object getItemData(int position) {
		try {
            Object item = null;
		    if (mListModelExtend != null) {
                item = mListModelExtend.interceptGetItemData(position);
                if (item != null) {
                    return item;
                }
            }
			int count = 0;
			if (null != mPullableView && mPullableView instanceof ListView) {
                count = ((ListView) mPullableView).getHeaderViewsCount();
            }
			int pos = position - count;
			if (pos >= 0) {
                return mDatas.get(pos);
            }
		} catch (Exception e) {
		}
		return null;
    }

    /**
     * 没有动画滑动
     *
     * @param position
     */
    public void setSelection(int position) {
        try {
            View mainView = mPullToRefLayout.getMainView();
            if (mainView != null && mainView instanceof AbsListView) {
                ((AbsListView) mainView).setSelection(position);
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "setSelection:报错啦:" + e.getMessage());
        }
    }

    /**
     * 带动画滑动
     *
     * @param position
     */
    public void smoothScrollToPosition(int position) {
		try {
            View mainView = mPullToRefLayout.getMainView();
            if (mainView != null && mainView instanceof AbsListView) {
                ((AbsListView) mainView).smoothScrollToPosition(position);
            }
		} catch (Exception e) {
			LogUtils.e(TAG, "smoothScrollToPosition:报错啦:" + e.getMessage());
		}
	}

	/**
	 * 隐藏头部或底部
	 *
	 * @param state
	 * @param msg
	 */
	public void hideHeaderFooter(int state, String msg) {
		if (mPullToRefLayout == null) {
			return;
		}
		if (mIsHideFooter) {
			mPullToRefLayout.loadmoreFinish(state, msg);
		}
		mPullToRefLayout.refreshFinish(state, msg);
	}

    /**
     * 展开ExpandableListView所有的节点
     */
	public void expandAllExpandable() {
        if (mListModelMessage.isExpan() && (getMainView() instanceof ExpandableListView)) {
            int count = mAdapter.getCount();
            for (int i = 0; i < count; i++) {
                ((ExpandableListView) getMainView()).expandGroup(i);
            }
        }
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object data = getItemData(position);
		if (data == null) {
			return;
		}
		if (null != mItemClickEvent) {
            /*JSONObject params = null;
            if (data instanceof JSONObject) {
                params = (JSONObject) data;
            } else {
                params = JSON.parseObject(JSON.toJSONString(data));
            }*/
            mItemClickEvent.initAllParams(data);
            mEventFactory.execEventMessage(view, mItemClickEvent, data);
        }
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(parent, view, position, id, data);
        }
        if (mListModelExtend != null) {
			mListModelExtend.onItemClick(parent, view, position, id, data);
		}
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Object data = getItemData(position);
        if (data == null) {
			return true;
		}
        Boolean result = null;
        if (mItemClickListener != null) {
             result = mItemClickListener.onItemLongClick(parent, view, position, id, data);
        }
        if (mListModelExtend != null) {
			result = mListModelExtend.onItemLongClick(parent, view, position, id, data);
		}
        return (result == null) ? true : result;
    }

	@Override
	public void onClick(View v, ReloadType reloadType) {
		super.onClick(v, reloadType);
		exec(true);
	}

    @Override
    public void onTouchNavigationChanged(String value) {
        try {
            if (mPullableView != null && mPullableView instanceof ListView && mAdapter instanceof INavigationAdapter) {
                ListView listView = (ListView) mPullableView;
                INavigationAdapter adapter = (INavigationAdapter) this.mAdapter;
                // 该字母首次出现的位置
                int position = adapter.getPositionForSection(value.charAt(0));
                if (position != -1) {
                    listView.setSelection(position + listView.getHeaderViewsCount());
                }
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "onTouchNavigationChanged:报错了:" + e.getMessage());
        }
    }

	@Override
	public boolean onLifecycle(Lifecycle lifecycle, Object... params) {
        boolean resultExtend = super.onLifecycle(lifecycle, params);
        boolean resultAdapter = true;
        if (mAdapter != null) {
            if (Lifecycle.onCreate == lifecycle) {
                mAdapter.onCreate((Bundle) params[0]);
            } else if (Lifecycle.onStart == lifecycle) {
                resultAdapter = mAdapter.onStart();
            } else if (Lifecycle.onRestart == lifecycle) {
                resultAdapter = mAdapter.onRestart();
            } else if (Lifecycle.onResume == lifecycle) {
                resultAdapter = mAdapter.onResume();
            } else if (Lifecycle.onStop == lifecycle) {
                resultAdapter = mAdapter.onStop();
            } else if (Lifecycle.onPause == lifecycle) {
                resultAdapter = mAdapter.onPause();
            } else if (Lifecycle.onDestroy == lifecycle) {
                resultAdapter = mAdapter.onDestroy();
            } else if (Lifecycle.onBackPressed == lifecycle) {
                resultAdapter = mAdapter.onBackPressed();
            } else if (Lifecycle.onAttach == lifecycle) {
                mAdapter.onAttach((Activity) params[0]);
            } else if (Lifecycle.onActivityCreated == lifecycle) {
                mAdapter.onActivityCreated((Bundle) params[0]);
            } else if (Lifecycle.onDestroyView == lifecycle) {
                resultAdapter = mAdapter.onDestroyView();
            } else if (Lifecycle.onDetach == lifecycle) {
                resultAdapter = mAdapter.onDetach();
            }
        }
		return resultExtend && resultAdapter;
	}

	/*=====================内部View 的滚动监听=======================*/
    /**
     * 内部View 的滚动监听
     *
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState){
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: {//停止滚动
                //设置为停止滚动
                mAdapter.setScrollState(false);
                mAdapter.notifyDataSetChanged();
                break;
            } case AbsListView.OnScrollListener.SCROLL_STATE_FLING: {//滚动做出了抛的动作
                //设置为正在滚动
                mAdapter.setScrollState(true);
                break;
            } case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: {//正在滚动
                //设置为正在滚动
                mAdapter.setScrollState(true);
                break;
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
	/*=====================内部View 的滚动监听=======================*/
}
